package com.jsmy.marketing.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.widget.TextView;

import com.jsmy.marketing.activity.LoginActivity;
import com.jsmy.marketing.base.BaseActivity;
import com.jsmy.marketing.manager.UserManager;

import io.reactivex.functions.Consumer;

/**
 * Created with Android Studio.
 * User: wei_cd.
 * Date: 2018/3/12.
 * Time: 17:06.
 * To change for file | settings | file and code templates.
 */

public class DialogUtils {

    public static void showLogOutDialog(final Context context) {
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_LIGHT);
        normalDialog.setTitle("下线提示:");
        normalDialog.setMessage("您的账号已经在另一台设备上登录，您被迫下线!");
        normalDialog.setCancelable(false);
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        ARouter.getInstance().build(AR.A_LOGIN).navigation();
//                        SPF.clear(context);
//                        ActivityManager.getInstance().removeAllActivity();
                        UserManager.getInstance().setIsLogin(false);
                        context.startActivity(new Intent(context,LoginActivity.class));
                        ActivityManager.getInstance().finishAll();
                    }
                });
        // 显示
        normalDialog.show();
    }

    public static void showLogBackDialog(final Context context) {
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_LIGHT);
        normalDialog.setTitle("温馨提示:");
        normalDialog.setMessage("您的确退出当前账户，使用另一账户登录吗？");
        normalDialog.setCancelable(false);
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        ARouter.getInstance().build(AR.A_LOGIN).navigation();
//                        SPF.clear(context);
                        dialog.dismiss();
                         SharedP.setIsLogin(false);
                        ActivityManager.getInstance().exit();
                    }
                });
        // 显示
        normalDialog.show();
    }

    public static void showCacheDialog(final Context context, String cache, final TextView tv) {
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_LIGHT);
        normalDialog.setTitle("当前缓存" + cache);
        normalDialog.setMessage("确定要清除缓存吗？");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        CacheDataManager.clearAllCache(context).subscribe(new Consumer<String>() {
                            @Override
                            public void accept(String s) throws Exception {
                                GlideApp.get(context).clearMemory();
                                CacheDataManager.getTotalCacheSize(context).subscribe(new Consumer<String>() {
                                    @Override
                                    public void accept(String s) throws Exception {
                                        tv.setText(s);
                                    }
                                });
                            }
                        });
//                        context.runOnUiThread(new Runnable() {
//                            @Override
//                            public void run() {
//                                tv.setText(CacheDataManager.getTotalCacheSize(context));
//                            }
//                        });

                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                    }
                });
        // 显示
        normalDialog.show();
    }

    public static void showVersionDialog(final BaseActivity context, String version, String msg, final String url) {
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_LIGHT);
        normalDialog.setTitle("更新至" + version);
        normalDialog.setMessage(msg);
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        context.goToDownLoadService(context, url);
                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                    }
                });
        // 显示
        normalDialog.show();
    }

//    public static void showDeleteDialog(final BaseActivity context, final DailyPresenter presenter, final String rid){
//        final AlertDialog.Builder normalDialog =
//                new AlertDialog.Builder(context);
//        normalDialog.setMessage("确定删除该条日志吗？");
//        normalDialog.setPositiveButton("确定",
//                new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        presenter.savedelgtrzinfo(rid);
//                    }
//                });
//        normalDialog.setNegativeButton("关闭",
//                new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        //...To-do
//                    }
//                });
//        // 显示
//        normalDialog.show();
//    }

}
